package api

import (
	"getaway/basic/config"
	__ "getaway/basic/proto/orderproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"github.com/gin-gonic/gin"
)

// 导出订单任务
func ExportOrder(c *gin.Context) {
	var req request.ExportOrderReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	// 调用GRPC服务获取任务状态
	resp, err := config.OrderClient.ExportOrder(c, &__.ExportOrderReq{
		UserId:    int32(c.GetUint("userId")),
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		OrderType: req.OrderType,
	})
	if err != nil {
		response.SrvError(c, "获取任务状态失败", err.Error())
		return
	}
	response.Success(c, "获取任务状态成功", map[string]interface{}{
		"task_id": resp.TaskId,
	})
}

// getStatusText 将状态码转换为可读文本
func getStatusText(status uint32) string {
	statusMap := map[uint32]string{
		0: "初始化",
		1: "处理中",
		2: "已完成",
		3: "失败",
	}
	if text, ok := statusMap[status]; ok {
		return text
	}
	return "未知状态"
}

// 获取导出任务列表
func GetExportTaskListOrder(c *gin.Context) {
	var req request.GetExportTaskListOrder
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.GetExportTaskListOrder(c, &__.GetExportTaskListOrderReq{
		UserId:    uint32(c.GetUint("userId")),
		Page:      req.Page,
		PageSize:  req.PageSize,
		Status:    req.Status,
		StartDate: req.StartDate,
		EndDate:   req.EndDate,
	})
	if err != nil {
		response.SrvError(c, "获取任务列表失败", err.Error())
		return
	}
	response.Success(c, "获取任务列表成功", resp)
}

// 删除导出任务
func DeleteExportTaskOrder(c *gin.Context) {
	var req request.DeleteExportTaskOrderReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.DeleteExportTaskOrder(c, &__.DeleteExportTaskOrderReq{
		TaskId: req.TaskId,
		UserId: uint32(c.GetUint("userId")),
	})
	if err != nil {
		response.SrvError(c, "删除任务失败", err.Error())
		return
	}
	response.Success(c, "删除任务成功", resp)
}

// 下载导出文件
func DownloadExportFileOrder(c *gin.Context) {
	var req request.DownloadExportFileOrderReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.DownloadExportFileOrder(c, &__.DownloadExportFileOrderReq{
		TaskId:        req.TaskId,
		UserId:        uint32(c.GetUint("userId")),
		EncryptionKey: req.EncryptionKey,
	})
	if err != nil {
		response.SrvError(c, "下载文件失败", err.Error())
		return
	}
	response.Success(c, "下载文件成功", resp)
}

// 获取导出进度
func GetExportProgressOrder(c *gin.Context) {
	var req request.GetExportProgressOrderReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.GetExportProgressOrder(c, &__.GetExportProgressOrderReq{
		TaskId: req.TaskId,
		UserId: uint32(c.GetUint("userId")),
	})
	if err != nil {
		response.SrvError(c, "获取进度失败", err.Error())
		return
	}
	response.Success(c, "获取进度成功", resp)
}
